﻿using System;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Configuration;
using System.Linq;

namespace SampleAppForAppConfigEncrypt
{
    class Functions
    {

        public static byte[] GetByteFromFile(string filepath)
        {
            byte[] filrebytes = null;
            if (File.Exists(filepath))
            {
                FileInfo fileinfo = new FileInfo(filepath);
                long filelength = fileinfo.Length;
                FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
                filrebytes = new byte[Convert.ToInt32(filelength)];
                int iBytesRead = fs.Read(filrebytes, 0, Convert.ToInt32(filelength));
                fs.Close();
            }
            return filrebytes;
        }


        /// <summary>
        /// take an int var and get it's string representaion
        /// </summary>
        /// <param name="m">int varaiable</param>
        /// <returns>مقدار رشته ای</returns>
        public string Into32ToString(int m)
        {
            return m.ToString();
        }


        public static Image GetImage(byte[] imagebytes)
        {
            Image img = null;
            if (imagebytes != null)
            {
                MemoryStream mstream = new MemoryStream(imagebytes);
                img = Image.FromStream(mstream);
            }
            return img;
        }

   
        public static string HashPass(string password)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] originbytes = ASCIIEncoding.Default.GetBytes(password);
            byte[] encodedbytes = md5.ComputeHash(originbytes);
            return BitConverter.ToString(encodedbytes);

        }

        public static string EncryptMD5(string normalText, string securityKey)
        {
            try
            {
                byte[] keyArray;
                byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(normalText);

                MD5CryptoServiceProvider cspMD5 = new MD5CryptoServiceProvider();
                keyArray = cspMD5.ComputeHash(UTF8Encoding.UTF8.GetBytes(securityKey));
                cspMD5.Clear();

                TripleDESCryptoServiceProvider TriDES = new TripleDESCryptoServiceProvider();
                TriDES.Key = keyArray;
                TriDES.Mode = CipherMode.ECB;
                TriDES.Padding = PaddingMode.PKCS7;

                ICryptoTransform cTransform = TriDES.CreateEncryptor();

                byte[] resultArray =
                  cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

                TriDES.Clear();

                return Convert.ToBase64String(resultArray, 0, resultArray.Length);
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message + "\n" + "Error in Encrypting");
            }


        }
        public static string DecryptMD5(string cipherText, string securityKey)
        {
            try
            {
                byte[] keyArray;
                byte[] toEncryptArray = Convert.FromBase64String(cipherText);

                MD5CryptoServiceProvider cspMD5 = new MD5CryptoServiceProvider();
                keyArray = cspMD5.ComputeHash(UTF8Encoding.UTF8.GetBytes(securityKey));

                cspMD5.Clear();

                TripleDESCryptoServiceProvider TriDES = new TripleDESCryptoServiceProvider();
                TriDES.Key = keyArray;
                TriDES.Mode = CipherMode.ECB;
                TriDES.Padding = PaddingMode.PKCS7;

                ICryptoTransform cTransform = TriDES.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

                TriDES.Clear();

                return UTF8Encoding.UTF8.GetString(resultArray);
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message + "\n" + "Error in Decrypting");
            }


        }



        public static string GetAppSettingByKey(string AppSettingKey)
        {
            string value = string.Empty;
            if (ConfigurationManager.AppSettings.AllKeys.Contains(AppSettingKey))
            {


                value = ConfigurationManager.AppSettings[AppSettingKey].ToString();
            }
            return value;
        }




        public static void SetAppSettingByKey(string AppSettingKey, string AppSettingValue)
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            // Add an Application Setting.
            if (!ConfigurationManager.AppSettings.AllKeys.Contains(AppSettingKey))
            {
                config.AppSettings.Settings.Add(AppSettingKey, AppSettingValue);
            }
            else
            {
                config.AppSettings.Settings[AppSettingKey].Value = AppSettingValue;
            }
            // Save the configuration file.
            config.Save(ConfigurationSaveMode.Modified);
            // Force a reload of a changed section.
            ConfigurationManager.RefreshSection("appSettings");
        }

    }
}
